﻿using Sidvall.Security;

namespace Sidvall.Data
{
    public class InitializeSettings
    {
        #region Public Members

        public FieldInitializeOption FieldInitializeOption { get; set; }
        public Sidvall.Business.FieldCopyOptions FieldCopyOption { get; set; }
        public RelationFieldInitializeOption ChildInitializeOption { get; set; }
        public RelationFieldInitializeOption ParentInitializeOption { get; set; }
        public bool ClearBusinessRules { get; set; }

        #region IncludedProperty

        public bool IncludedProperty(IPropertyAccessLevelProvider accessLevelProvider, bool isKeyProperty, bool propertyIsInRelation, bool isRelationProperty,
            Sidvall.Data.DataType propertyDataType, string propertyName, string entityName)
        {
            if (_Evaluator != null)
                return _Evaluator.CanAccess(accessLevelProvider, isKeyProperty, propertyIsInRelation, isRelationProperty, propertyDataType, propertyName, entityName);
            return true;
        }

        #endregion
        #region SetEvaluator

        public void SetEvaluator(IPropertyAccessLevelEvaluator accessLevelEvaluator)
        {
            _Evaluator = accessLevelEvaluator;
        }

        #endregion

        #endregion
        #region Factory Members

        #region CreateInitialization

        public static InitializeSettings CreateInitialization(FieldInitializeOption fieldOption, bool clearBusinessRules, Sidvall.Business.FieldCopyOptions fieldCopyOption,
            RelationFieldInitializeOption childOption, RelationFieldInitializeOption parentOption)
        {
            return new InitializeSettings(fieldOption, fieldCopyOption, childOption, parentOption, clearBusinessRules);
        }

        #endregion
        #region FullInitialization

        public static InitializeSettings FullInitialization(FieldInitializeOption fieldOption, bool clearBusinessRules)
        {
            return new InitializeSettings(fieldOption, Sidvall.Business.FieldCopyOptions.All, RelationFieldInitializeOption.Include, RelationFieldInitializeOption.Include,
                clearBusinessRules);
        }
        public static InitializeSettings FullInitialization(FieldInitializeOption fieldOption, bool clearBusinessRules, Sidvall.Business.FieldCopyOptions fieldCopyOption)
        {
            return new InitializeSettings(fieldOption, fieldCopyOption, RelationFieldInitializeOption.Include, RelationFieldInitializeOption.Include, clearBusinessRules);
        }

        #endregion
        #region ItemInitialization

        public static InitializeSettings ItemInitialization(FieldInitializeOption fieldOption, bool clearBusinessRules)
        {
            return new InitializeSettings(fieldOption, Sidvall.Business.FieldCopyOptions.All, RelationFieldInitializeOption.None, RelationFieldInitializeOption.None,
                clearBusinessRules);
        }
        public static InitializeSettings ItemInitialization(FieldInitializeOption fieldOption, bool clearBusinessRules, Sidvall.Business.FieldCopyOptions fieldCopyOption)
        {
            return new InitializeSettings(fieldOption, fieldCopyOption, RelationFieldInitializeOption.None, RelationFieldInitializeOption.None, clearBusinessRules);
        }

        #endregion
        #region RelationInitialization

        public static InitializeSettings RelationInitialization(RelationFieldInitializeOption childOption, RelationFieldInitializeOption parentOption)
        {
            return new InitializeSettings(FieldInitializeOption.None, Sidvall.Business.FieldCopyOptions.None, childOption, parentOption, false);
        }
        public static InitializeSettings RelationInitialization(RelationFieldInitializeOption childOption, RelationFieldInitializeOption parentOption, bool clearBusinessRules)
        {
            return new InitializeSettings(FieldInitializeOption.None, Sidvall.Business.FieldCopyOptions.None, childOption, parentOption, clearBusinessRules);
        }

        #endregion

        #endregion
        #region Private Members

        private IPropertyAccessLevelEvaluator _Evaluator;

        #endregion
        #region Constructors

        public InitializeSettings()
            : this(FieldInitializeOption.Reset, Sidvall.Business.FieldCopyOptions.All, RelationFieldInitializeOption.None, RelationFieldInitializeOption.None, false)
        {
        }
        public InitializeSettings(FieldInitializeOption fieldOption, Sidvall.Business.FieldCopyOptions fieldCopyOption,
            RelationFieldInitializeOption childOption, RelationFieldInitializeOption parentOption, bool clearBusinessRules)
        {
            this.FieldInitializeOption = fieldOption;
            this.FieldCopyOption = fieldCopyOption;
            this.ChildInitializeOption = childOption;
            this.ParentInitializeOption = parentOption;
            this.ClearBusinessRules = clearBusinessRules;
        }

        #endregion
    }
}
